﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;

namespace Activity.Helper
{
    public static class ExportHelper
    {
        /// <summary>  
        /// 设置字体格式  
        /// </summary>  
        /// <param name="doc"></param>  
        /// <param name="table"></param>  
        /// <param name="setText"></param>  
        /// <returns></returns>  
        public static XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText)
        {
            try
            {
                //table中的文字格式设置  
                CT_P para = new CT_P();
                XWPFParagraph pCell = new XWPFParagraph(para, table.Body);
                pCell.Alignment = ParagraphAlignment.CENTER; //字体居中  
                pCell.VerticalAlignment = NPOI.XWPF.UserModel.TextAlignment.CENTER; //字体居中  

                XWPFRun r1c1 = pCell.CreateRun();
                r1c1.SetText(setText);
                r1c1.FontSize = 12;
                //r1c1.SetFontFamily("华文楷体", FontCharRange.None);//设置雅黑字体  
                r1c1.FontFamily = "华文楷体";
                //r1c1.SetTextPosition(20);//设置高度  

                return pCell;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw (ex);
            }
        }

        /// <summary>  
        /// 设置单元格格式  
        /// </summary>  
        /// <param name="doc">doc对象</param>  
        /// <param name="table">表格对象</param>  
        /// <param name="setText">要填充的文字</param>  
        /// <param name="align">文字对齐方式</param>  
        /// <param name="textPos">rows行的高度</param>  
        /// <returns></returns>  
        public static XWPFParagraph SetCellText(XWPFDocument doc, XWPFTable table, string setText,
            ParagraphAlignment align, TextAlignment textAlign = TextAlignment.AUTO, int textPos = 20,
            bool isBold = false, int fontSize = 12, string fontFamily = "宋体", string rbgStr = "#000000")
        {
            try
            {
                var para = new CT_P();
                var pCell = new XWPFParagraph(para, table.Body);
                pCell.Alignment = align;
                pCell.VerticalAlignment = textAlign;

                var run = pCell.CreateRun();
                run.SetText(setText);
                run.FontSize = fontSize;
                run.SetColor(rbgStr);
                run.FontFamily = fontFamily;
                run.SetTextPosition(textPos); //设置高度  
                run.SetBold(isBold);
                return pCell;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw (ex);
            }
        }

        public static XWPFParagraph SetCellPicture(XWPFDocument doc, XWPFTable table, IList<string> pictureUrl, IList<string> pictureName,
            ParagraphAlignment align, int width, int height)
        {
            try
            {
                CT_P para = new CT_P();
                var pCell = new XWPFParagraph(para, table.Body);
                pCell.Alignment = align;
                var run = pCell.CreateRun();
                var MyWebClient = new WebClient();

                var strPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //获取或设置包含该应用程序的目录的名称。

                if (!Directory.Exists(strPath + "oosPicture"))
                {
                    Directory.CreateDirectory(strPath + "oosPicture"); //不存在就创建文件夹 }
                }

                var dyInfo = new DirectoryInfo(strPath + "oosPicture");

                var fileName = $"{pictureName[0]}.jpg";

                if (!File.Exists($"{strPath}\\oosPicture\\{fileName}"))
                {
                    Byte[] pageData = MyWebClient.DownloadData(pictureUrl[0]);
                    File.WriteAllBytes($"{strPath}\\oosPicture\\{fileName}", pageData);
                }
                using (FileStream fsImg = new FileStream($"{strPath}\\oosPicture\\{fileName}", FileMode.Open,
                    FileAccess.Read))
                {
                    run.AddPicture(fsImg, (int)PictureType.JPEG, fileName, width, height);
                }
                for (var i = 1; i < pictureUrl.Count(); i++)
                {
                    if (!string.IsNullOrEmpty(pictureUrl[i]))
                    {
                        run = pCell.CreateRun();

                        var fileNameItem = $"{pictureName[i]}.jpg";

                        if (!File.Exists($"{strPath}\\oosPicture\\{fileNameItem}"))
                        {
                            Byte[] pageData = MyWebClient.DownloadData(pictureUrl[i]);
                            File.WriteAllBytes($"{strPath}\\oosPicture\\{fileNameItem}", pageData);
                        }
                        using (FileStream fsImg = new FileStream($"{strPath}\\oosPicture\\{fileNameItem}",
                            FileMode.Open,
                            FileAccess.Read))
                        {
                            run.AddPicture(fsImg, (int) PictureType.JPEG, fileNameItem, width, height);
                        }
                    }
                }
                //获取文件夹下所有的文件
                foreach (FileInfo feInfo in dyInfo.GetFiles())
                {
                    //判断文件日期是否小于2小时，是则删除
                    if (feInfo.CreationTime < DateTime.Now.AddHours(-1))
                        feInfo.Delete();
                }
                return pCell;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw (ex);
            }
        }
    }
}
